<?php

namespace app\admin\controller;
use app\common\model\Category;

/**
 * 文章管理控制器
 * Class Article
 * @package app\admin\controller
 */
class Article extends Common
{
    private $db;
    public function __construct()
    {
        parent::__construct();
        $this->db = new \app\common\model\Article();
    }
    //首页方法
    public function index(){
        //手册搜索  数据库->查询构造器->链式操作->join 实现多表关联
        $data = db('article')
                ->alias('a')
                ->join('__CATEGORY__ c','a.category_cid = c.cid')
                ->order('sendtime desc')
                ->where('is_recycle',0)
                ->paginate(8);
        $this->assign('data',$data);
        return view();
    }
    //添加方法
    public function add(){
        //2.获取所有分类
        $cateData =  (new \app\common\model\Category())->getCateData();
        $this->assign('cateData',$cateData);
        //3.获取所有标签
        $cateTag = db('tag')->select();
        $this->assign('cateTag',$cateTag);
        //4.执行添加
        if(request()->isPost()){
            $res = $this->db->add(input('post.'));
            if ($res['valid']) {
                $this->success($res['msg'], 'admin/article/index');
            } else {
                $this->error($res['msg']);
            }
        }
        //1.载入视图
        return view();
    }
    //回收站
    public function recycle(){
        $aid = input('param.aid');
        $res = $this->db->recycle($aid);
        if(isset($aid) && !$res['valid']){
            $this->error($res['msg']);
        }
            $data = db('article')
                ->alias('a')
                ->join('__CATEGORY__ c','a.category_cid = c.cid')
                ->order('sendtime desc')
                ->where('is_recycle',1)
                ->paginate(8);
            $this->assign('data',$data);

        return view();
    }
    public function delete(){
        $aid = input('param.aid');
        $row = db('article')->delete($aid);
        if($row){
            db('article_tag')->where('article_aid',$aid)->delete();
            db('article_data')->where('article_aid',$aid)->delete();
            $this->success('删除成功','recycle');
        }else{
            $this->error('删除失败');
        }
    }

    //编辑方法
    public function edit(){
        $aid = input('param.aid');
        //2.获取旧数据
        $oldData = db('article')
                 ->alias('a')
                 ->join('__ARTICLE_DATA__ ad','a.aid = ad.article_aid')
                 ->find($aid);
        $this->assign('oldData',$oldData);

        //3.获取所属分类
        $cateDate = (new Category())->getCateData();
        $this->assign('cateData',$cateDate);

        //4.获取所有标签
        $tagData = db('tag')->select();
        $this->assign('tagData',$tagData);

        //5.获取选中的标签id  操作文章标签中间表
        $checkTid = db('article_tag')->where('article_aid',$aid)->column('tag_tid');
        $this->assign('checkTid',$checkTid);

        //6.执行编辑
        //文章表正常编辑，文章标签中间表先删后添，文章数据表根据aid进行编辑
        if(request()->isPost()){
            $res = $this->db->edit(input('post.'));
            if ($res['valid']) {
                $this->success($res['msg'], 'admin/article/index');
            } else {
                $this->error($res['msg']);
            }
        }

        //1.载入模版
        return view();
    }
}
